//
//  TJFriendsController.m
//  CrossBorderPayment
//
//  Created by hc-101 on 2020/6/9.
//  Copyright © 2020 xtjun. All rights reserved.
//

#import "TJFriendsController.h"
#import "TJFriendsListCell.h"
#import "TJTransferController.h"

@interface TJFriendsController ()

@property (strong, nonatomic) NSArray *datas;
@property (strong, nonatomic) NSArray *originalDatas;
@property (weak, nonatomic) UITextField *searchTextField;

@end

static NSString * const friendsListCell = @"friendsListCell";

@implementation TJFriendsController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationBar.rightBarButton = [TJButtonBarButton barButtonWithImageName:@"addFriend_icon" target:self action:@selector(didClickRightButton)];
    
    UIView *searchContentView = [[UIView alloc] init];
    [self.view addSubview:searchContentView];
    searchContentView.backgroundColor = UIColor_Hex(0xF4F4F4);
    [searchContentView shearRoundedCornersWithRadiu:3];
    [searchContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16);
        make.right.offset(-16);
        make.top.offset(KNavBarHight + 10);
        make.height.offset(40);
    }];
    
    
    UIImageView *searchImageView = [UIImageView tj_addToView:searchContentView];
    searchImageView.image = [UIImage imageNamed:@"icon-search"];
    [searchImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16);
        make.centerY.offset(0);
        make.width.height.offset(20);
    }];
    
    UITextField *searchTextField = [UITextField tj_addToView:searchContentView];
    searchTextField.font = UIFont_Px(15);
    searchTextField.placeholder = @"查找您的好友".localizedString;
    [searchTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    searchTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    searchTextField.returnKeyType = UIReturnKeySearch;
    [searchTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(searchImageView.mas_right).offset(16);
        make.right.offset(-5);
        make.centerY.offset(0);
        make.height.offset(30);
    }];
    
    
    self.verticalOffset = 0;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.rowHeight = 60;
    self.tableView.backgroundColor = UIColor.whiteColor;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:TJFriendsListCell.self forCellReuseIdentifier:friendsListCell];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(searchContentView.mas_bottom).offset(5);
        make.left.right.bottom.offset(0);
    }];
    
    _searchTextField = searchTextField;
    [self loadData];
    
 }

- (void)textFieldDidChange:(UITextField *)textField {
    
    
    if (_searchTextField.text.length) {
        NSMutableArray *Marr = @[].mutableCopy;
        [self.originalDatas enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj[@"account"] containsString:textField.text]) {
                [Marr addObject:obj];
            }
        }];
        
        if (Marr.count == self.datas.count) return;
        self.datas = Marr;
        [self.tableView reloadData];
    } else {
        
        self.datas = self.originalDatas;
        [self.tableView reloadData];
    }
    
}
 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TJFriendsListCell *cell = [tableView dequeueReusableCellWithIdentifier:friendsListCell forIndexPath:indexPath];
    cell.dict = self.datas[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setTapTransferLabelBlock:^(NSString * _Nonnull userId) {
       TJTransferController *transferController = [[TJTransferController alloc] init];
//        transferController.dict = @{@"userId": userId};
        [self.navigationController pushViewController:transferController animated:YES];
    }];
    
    return cell;
}

- (void)loadData {
    
    [TJNetworking.shareNetworking POST:@"user/getFriends" parameters:@{@"token": TJUserInfo.readInfo.token} success:^(NSDictionary * _Nonnull responseObject, NSArray *data) {
        
        self.displayEmpty = YES;
        
        if (data.count) {
            self.datas = data;
            self.originalDatas = [NSArray arrayWithArray:data];
        }
        
        [self.tableView reloadData];
        
    } failure:^(NSError * _Nonnull error) {
        self.displayEmpty = YES;
        [self.tableView reloadData];

    }];
}

- (void)didClickRightButton {
    [self.navigationController pushViewControllerWithName:@"TJSearchFriendsController" title:nil animated:YES];
}

 
@end
